import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.GEItemInfo;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSInterfaceChild;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;

/*
 * My appologies to all users who bought this script from me
 * I appreciate your business, but script sales are no longer
 * permitted, and this script is leeched and leaked, so I see
 * no point in keeping it private any longer.
 */
public class AEPizzaBuyer extends Script implements PaintListener {
	public final int INV_GOLD = 995;
	public final int INV_PIZZA = 2289;

	public final int IFACE_FOODSHOP = 620;
	public final int CHILD_CLOSE_SHOP = 7;

	public int shopKeeperID = 4293; // Trade
	public int bankBoothID = 2213; // Use quickly
	public RSTile toShop = new RSTile(2840, 3551);
	public RSTile toBank = new RSTile(2846, 3542);
	public Rectangle itemBox = new Rectangle(148, 104, 16, 16); // Pizza

	public int inventoryItem = INV_PIZZA;
	public int itemCount = 0;
	public long startTime;
	public int randomRunEnergy = random(20, 60);
	public int itemCost = 200;
	public GEItemInfo pizzaInfo = grandExchange.loadItemInfo(INV_PIZZA);

	public boolean closeShop() {
		final RSInterfaceChild closeButton = RSInterface.getChildInterface(
				IFACE_FOODSHOP, CHILD_CLOSE_SHOP);

		if (closeButton != null) {
			return atInterface(closeButton);
		} else {
			return false;
		}
	}

	@Override
	public String getAuthor() {
		return "Aelin";
	}

	@Override
	public String getName() {
		return "Aelin's Pizza Buyer";
	}

	@Override
	public String getScriptCategory() {
		return "Money";
	}

	@Override
	public double getVersion() {
		return 1.01;
	}

	public boolean isShopOpen() {
		return RSInterface.getInterface(IFACE_FOODSHOP).isValid();
	}

	@Override
	public int loop() {
		final RSObject bankBooth = findObject(bankBoothID);
		final RSNPC shopKeeper = getNearestNPCByID(shopKeeperID);

		if (getMyPlayer().isMoving()) {
			return random(750, 1000);
		}

		if (isShopOpen() && !isInventoryFull()) {
			clickMouse(itemBox.x, itemBox.y, itemBox.width, itemBox.height,
					false);
			wait(random(750, 1000));
			atMenu("Buy 10");
			return random(750, 1000);
		}

		if (RSInterface.getInterface(Constants.INTERFACE_BANK).isValid()) {
			itemCount += getInventoryCount(inventoryItem);
			bank.depositAllExcept(INV_GOLD);
			bank.close();
			return random(750, 1000);
		}

		if (randomRunEnergy <= getEnergy() && !isRunning()) {
			setRun(true);
			randomRunEnergy = random(20, 60);
			return random(1000, 2000);
		}

		if (isInventoryFull()) {
			if (isShopOpen()) {
				closeShop();
				return random(750, 1000);
			}

			if (tileOnScreen(bankBooth.getLocation())) {
				atObject(bankBooth, "Use-quickly");
				return random(750, 1000);
			} else {
				walkTileMM(randomizeTile(toBank, 2, 2));
				return random(750, 1000);
			}
		} else {
			log("Walking to shop");
			if (tileOnScreen(shopKeeper.getLocation())) {
				atNPC(shopKeeper, "Trade");
				return random(750, 1000);
			} else {
				walkTileMM(randomizeTile(toShop, 2, 2));
				return random(750, 1000);
			}
		}
	}

	@Override
	public void onFinish() {
		// Remove listeners.
		Bot.getEventManager().removeListener(PaintListener.class, this);
	}

	public void onRepaint(final Graphics g) {
		long seconds = 0;
		long minutes = 0;
		long hours = 0;
		int moneySpent;
		int profit;

		String runtimeString = "";

		seconds = (System.currentTimeMillis() - startTime) / 1000;
		if (seconds >= 60) {
			minutes = seconds / 60;
			seconds -= minutes * 60;
		}
		if (minutes >= 60) {
			hours = minutes / 60;
			minutes -= hours * 60;
		}

		runtimeString = hours + " hours " + minutes + " minutes and " + seconds
				+ " seconds";

		moneySpent = itemCount * itemCost;
		profit = itemCount * pizzaInfo.getMaxPrice() - moneySpent;

		g.setColor(new Color(255, 0, 0, 75));
		g.fillRoundRect(3, 264, 260, 72, 5, 5);

		g.setColor(Color.WHITE);
		g.drawString("Aelin's Pizza Buyer", 12, 280);
		g.drawString("Number of Pizzas: " + itemCount, 12, 296);
		g.drawString("Runtime: " + runtimeString, 12, 312);
		g.drawString("Profit: " + profit, 12, 328);
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		startTime = System.currentTimeMillis();
		return true;
	}
}